//
//  BankInfoInputVC.swift
//  LeGou
//
//  Created by lxm on 2022/9/4.
//

import UIKit

class BankInfoInputVC: ZMViewController {
    //MARK: --------------------属性--------------------
    
    //MARK: --------------------UI控件--------------------
    let xm = ZMUtil.label(title: "持卡人", color: UIColor.zm.color333333(), font: UIFont.systemFont(ofSize: 14))
    
    let nameContent = UIView().then({$0.backgroundColor = .white;$0.layer.cornerRadius = 12})

    let nameText = UITextField().then { t in
        t.font = UIFont.systemFont(ofSize: 14)
        t.MaxCharcterCount = 20
        t.attributedPlaceholder = "请填写持卡人姓名".zm.attributedStr(attrs: [.foregroundColor : "#999999".zm.hex()])
        t.isUserInteractionEnabled = false
    }
    
    let sfzh = ZMUtil.label(title: "身份证号", color: UIColor.zm.color333333(), font: UIFont.systemFont(ofSize: 14))
    
    let idcardContent = UIView().then({$0.backgroundColor = .white;$0.layer.cornerRadius = 12})

    let idcardText = UITextField().then { t in
        t.font = UIFont.systemFont(ofSize: 14)
        t.MaxCharcterCount = 20
        t.attributedPlaceholder = "请填写18位身份证号".zm.attributedStr(attrs: [.foregroundColor : "#999999".zm.hex()])
        
        t.keyboardType = .asciiCapable
        t.delegate = InputMananger.shard.noChinese
        t.isUserInteractionEnabled = false
    }
    
    let yhkh = ZMUtil.label(title: "银行卡号", color: UIColor.zm.color333333(), font: UIFont.systemFont(ofSize: 14))
    
    let bankNumContent = UIView().then({$0.backgroundColor = .white;$0.layer.cornerRadius = 12})

    let bankNumText = UITextField().then { t in
        t.font = UIFont.systemFont(ofSize: 14)
        t.MaxCharcterCount = 20
        t.attributedPlaceholder = "请填写银行卡号".zm.attributedStr(attrs: [.foregroundColor : "#999999".zm.hex()])
        t.keyboardType = .numberPad
        t.delegate = InputMananger.shard.onlyNumber
    }
    
    
    let ssyh = ZMUtil.label(title: "预留手机号", color: UIColor.zm.color333333(), font: UIFont.systemFont(ofSize: 14))
    
    let bankContent = UIView().then({$0.backgroundColor = .white;$0.layer.cornerRadius = 12})

    let bankText = UITextField().then { t in
        t.font = UIFont.systemFont(ofSize: 14)
        t.MaxCharcterCount = 11
        t.keyboardType = .phonePad
        t.delegate = InputMananger.shard.onlyNumber
        t.attributedPlaceholder = "请填写预留手机号".zm.attributedStr(attrs: [.foregroundColor : "#999999".zm.hex()])
    }
    
    
    
    
    
    // 确认按钮
    let confirmButton = ZMUtil.button(title: "下一步", font: UIFont.systemFont(ofSize: 16), color: .white).then({$0.backgroundColor = Style.mainColor;$0.layer.cornerRadius = 23})
    //MARK: --------------------生命周期--------------------
    override func viewDidLoad() {
        super.viewDidLoad()
        setUI()
        addConstraints()
        bindAction()
    }
    //MARK: --------------------UI--------------------
    func setUI() {
        title = "添加银行卡"
        view.backgroundColor = "#F5F5F5".zm.hex()
        
        view.addSubview(xm)
        xm.frame = CGRect(x: 16, y: 16, width: SCREEN_W - 32, height: 0)
        xm.sizeToFit()
        
        view.addSubview(nameContent)
        nameContent.frame = CGRect(x: 16, y: xm.zm.bottom + 16, width: SCREEN_W - 32, height: 48)
        
        nameContent.addSubview(nameText)
        nameText.frame = CGRect(x: 12, y: 0, width: nameContent.zm.width, height: 48)
        
        view.addSubview(sfzh)
        sfzh.frame = CGRect(x: 16, y: nameContent.zm.bottom + 16, width: SCREEN_W - 32, height: 0)
        sfzh.sizeToFit()
        
        view.addSubview(idcardContent)
        idcardContent.frame = CGRect(x: 16, y: sfzh.zm.bottom + 16, width: SCREEN_W - 32, height: 48)
        
        idcardContent.addSubview(idcardText)
        idcardText.frame = CGRect(x: 12, y: 0, width: nameContent.zm.width, height: 48)
        
        // 银行卡号
        view.addSubview(yhkh)
        yhkh.frame = CGRect(x: 16, y: idcardContent.zm.bottom + 16, width: SCREEN_W - 32, height: 0)
        yhkh.sizeToFit()
        
        view.addSubview(bankNumContent)
        bankNumContent.frame = CGRect(x: 16, y: yhkh.zm.bottom + 16, width: SCREEN_W - 32, height: 48)
        
        bankNumContent.addSubview(bankNumText)
        bankNumText.frame = CGRect(x: 12, y: 0, width: bankNumContent.zm.width, height: 48)
        
        // 银行
        view.addSubview(ssyh)
        ssyh.frame = CGRect(x: 16, y: bankNumContent.zm.bottom + 16, width: SCREEN_W - 32, height: 0)
        ssyh.sizeToFit()
        
        view.addSubview(bankContent)
        bankContent.frame = CGRect(x: 16, y: ssyh.zm.bottom + 16, width: SCREEN_W - 32, height: 48)
        
        bankContent.addSubview(bankText)
        bankText.frame = CGRect(x: 12, y: 0, width: bankContent.zm.width, height: 48)
        
                
        view.addSubview(confirmButton)
        confirmButton.frame = CGRect(x: 16, y: bankContent.zm.bottom + 48, width: SCREEN_W - 32, height: 46)

        
    }
    
    
    func addConstraints() {
        
    }
    
    //MARK: --------------------methods--------------------
    func bindAction() {
        let nameOk = nameText.rx.text.orEmpty.map({$0.count >= 2})
//        let idOk = idcardText.rx.text.orEmpty.map({$0.zm.isIDCardNum()})
        let bankNumOk = bankNumText.rx.text.orEmpty.map({$0.zm.isBankCarNum()})
        let bankNameOk = bankText.rx.text.orEmpty.map({$0.zm.isPhoneNum()})
        
        Observable.combineLatest(nameOk, bankNumOk, bankNameOk).map({$0 && $1 && $2}).subscribe(onNext:{ [unowned self] resp in
            self.confirmButton.isEnabled = resp
            self.confirmButton.backgroundColor = resp ? Style.mainColor : Style.disableColor
        }).disposed(by: disposeBag)
        
        
        confirmButton.rx.tap.subscribe(onNext:{ [unowned self] resp in
            self.bankVerify()
        }).disposed(by: disposeBag)
        
        ZMAppShareData.share.userInfo.subscribe(onNext:{ [unowned self] resp in
            guard let resp = resp else {return}
            self.idcardText.text = resp.cardId.zm.hideIdNum()
            self.nameText.text = resp.realName
            self.idcardText.sendActions(for: .valueChanged)
            self.nameText.sendActions(for: .valueChanged)
        }).disposed(by: disposeBag)
    }
    
    func bankVerify() {
        let pa = [
            "bankMobile" : bankText.text!,
            "bankNo" : bankNumText.text!,
            "cardNo" : ZMAppShareData.share.safeUserInfo.cardId,
            "ownerName" : nameText.text!
        ] as [String : Any]
        ZMToast.showActivity()
        MineAPI.bankVerify(pa).rxRequest(type: Bool.self).subscribe(onNext:{ [unowned self] resp in
            if resp {
                ZMToast.hiddenActivity()
                let vc = TradeFroget()
                vc.type = 1
                vc.pa = pa
                vc.phoneStr = self.bankText.text!
                self.navigationController?.pushViewController(vc, animated: true)
            } else {
                ZMToast.showToast(title: "校验出错")
            }
        }).disposed(by: disposeBag)
    }

    //MARK: --------------------delegate--------------------
}
